package cn.edu.cqyz.wisdom.exception;

import cn.edu.cqyz.wisdom.entity.R;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.io.IOException;

/**
 * @Description TODO
 * @Author CDHong
 * @Date Created in 2024/10/23 11:45
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 静态资源找不到
     * @param resp
     * @throws IOException
     */
    @ExceptionHandler(NoResourceFoundException.class)
    public void notFound(HttpServletResponse resp) throws IOException {
      resp.sendRedirect("/backend/page/error/404.html");
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R exception(Exception ex){
        log.debug("服务器异常："+ex.getMessage());
        return R.error("服务器异常，请联系管理员");
    }

}
